import qs from 'qs'
export default {
    name: 'Userdevicegrant',
    components: {},
    data() {
        return {
            height: '600',
            userdevicegrantData: [],
            total: 0,
            pagesize: 5,
            currentPage: 1,
            multipleSelection: [],
            handleDeviceDialog: false,
            clientId: '',
            userGrantData:[],
            formDisabled:false,
            formLabelWidth:'120px',
            userData:[],
        }
    },
    computed: {
        offset: function () {
            return (this.currentPage - 1) * this.pagesize;
        },
    },
    watch: {
      handleDeviceDialog:function(newVal,oldVal){
          this.formDisabled = true;
      }
    },
    created() {
        this.initgrantData();
        this.initUserData();
    },
    mounted() {

    },
    methods: {
        //列表查询
        initgrantData() {
            this.$http.get('/userProDevicesController/queryUserDevice?limit='+this.pagesize+"&offset="+this.offset)
                .then((res) => {
                    this.userdevicegrantData = res.result;
                    this.total = res.total;
                }).catch((err) => {
                    console.log(err);
            });
        },
        //获取所有用户
        initUserData(){
          this.$http.get('/userController/querySubordinateUser')
            .then((res) => {
              this.userData = res.rows;
            }).catch((err) => {
                console.log(err);
          });
        },
        //绑定设备和解绑设备
        commitBtn() {
          var postData = {
            deviceId : this.clientId,
            uIds : this.userGrantData.join(",")
          }
          this.$http.post("/userProDevicesController/bindDevice",qs.stringify(postData))
           .then((res) => {
               if(res.status == 200){
                 this.$message({
                     type:'success',
                     message:res.message,
                 });
                 this.initgrantData();
                 this.handleDeviceDialog  = false;
                 return;
               } else if(res.status == 500){
                 this.$message.error(res.message);
               }
             }).catch((err) => {
                console.log(err);
           })
        },
        //解绑和绑定设备回显
        UntyingRow(row) {
          this.clientId = row.dId;
          this.userGrantData = row.userIds;
          this.handleDeviceDialog  = true;
        },
        handleSelectionChange(val) {
            this.multipleSelection = [];
        },
        handleSizeChange(val) {
            this.pagesize = val;
            this.initgrantData();
        },
        handleCurrentChange(val) {
            this.currentPage = val;
            this.initgrantData();
        }
    },
}